package com.guosheng.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.guosheng.dao.SysUserMapper;
import com.guosheng.pojo.ActiveUser;
import com.guosheng.pojo.SysCardInfo;
import com.guosheng.pojo.SysUser;
import com.guosheng.pojo.SysUserExample;
import com.guosheng.service.SysCardService;
import com.guosheng.service.SysUserService;

@Controller
@RequestMapping("/account")
public class AccountController {
	@Autowired
	SysCardService sysCardService;
	@Autowired
	SysUserService  sysUserService;
	
	@Autowired
	SysUserMapper sysUserMapper;
	
	//账户管理
	@RequestMapping("/managerAccount")
	public ModelAndView manager(String userId){
		  ModelAndView m=new ModelAndView();
		  if(StringUtils.isEmpty(userId)){
			  Session session=  SecurityUtils.getSubject().getSession();
			  ActiveUser activeUser=(ActiveUser)	session.getAttribute("activeUser");
		      userId=activeUser.getUserid();
		  }
			List<SysCardInfo>list=  sysCardService.findByUserId(userId);
			SysUserExample sysUserExample=new SysUserExample();
			SysUserExample.Criteria criteria= sysUserExample.createCriteria();
			criteria.andIdEqualTo(userId);
			SysUser sysUser=sysUserMapper.selectByExample(sysUserExample).get(0);
			for(SysCardInfo s:list){
				if(s.getAvailabel()==SysCardInfo.CARD_STATUS_FREEZE){
					s.setCardAvailabel("不可用");
				}else{
					s.setCardAvailabel("可用");
				}
			}
	        m.addObject("list", list);
	        m.addObject("customer", sysUser);
			m.setViewName("/account/accountList");
			return m;
	}
	@RequestMapping("addAccountView")
	public ModelAndView addAccountView(String id){
		  ModelAndView m=new ModelAndView();
	SysUserExample sysUserExample=new SysUserExample();
	SysUserExample.Criteria criteria= sysUserExample.createCriteria();
	criteria.andIdEqualTo(id);
	SysUser sysUser=sysUserMapper.selectByExample(sysUserExample).get(0);
	 m.addObject("sysUser", sysUser);
	 m.setViewName("/account/addAccountView");
	 return m;
	}
	
	@RequestMapping("addAccount")
	public String addAccount(SysCardInfo sysCardInfo){
	int i= sysCardService.insertAccount(sysCardInfo);
	if(i>0){
		return "redirect:/account/managerAccount.action";
	}else{
		return "/error";
	  }
	}
	@RequestMapping("freeze")
	public String accountFreeae(String cardId,String userid){
		//修改状态
	int i=	sysCardService.freezeAccount(cardId);
		if(i>0){
			return "redirect:/account/managerAccount.action?userId="+userid;
		}else{
			return "error";
		}
		
	}
	
	

}
